package com.example.thread2.thread4;

import java.util.concurrent.TimeUnit;

import static jdk.nashorn.internal.objects.Global.print;


/**
 * 后台线程，程序运行时在后台给i共一种通用服务的线程，所有非后台线程结束时，该后台线程终止
 * 后台线程的finally代码块中的代码不会被执行，
 */
public class SimpleDaemons implements Runnable{

    @Override
    public void run() {
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(100);
                print(Thread.currentThread() + " " + this);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            print("sheep() interrupted");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for(int i=0; i<10; i++){
            Thread daemon = new Thread(new SimpleDaemons());
            daemon.setDaemon(true);
            daemon.start();
        }
        print("All daemons started");
        TimeUnit.MILLISECONDS.sleep(175);
    }
}
